home *** CD-ROM | disk | FTP | other *** search
- Path: in1.uu.net!demos!usenet
- From: Alexey Ruzin <00alex@dbs.demos.su>
- Newsgroups: comp.lang.c++
- Subject: Re: BC 3.1 - 4.5 (error!?) ATTENTION
- Date: Thu, 18 Apr 1996 22:32:05 +0400
- Organization: Demos Online Service
- Message-ID: <31768AA5.420E@dbs.demos.su>
- References: <3173D628.48F6@demos.su> <4l5gn1$gtb@Grouper.Exis.Net>
- NNTP-Posting-Host: 00alex@dbs.demos.su
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0 (X11; I; SunOS 5.4 sun4m)
-
- Tim Lawrence wrote:
- >
- > Tested in BC 4.5 and 5.0. Results where what I expected. The reason you
- > may think you are getting unpredictable results is the use of a static var
- > in a function.
- > In article <3173D628.48F6@demos.su>, 00alex@demos.su says...
- > >
- > >Hi, All
- > >
- > >Here is simple example of abnormal programming.
- > >But BC crashed (sorry, it works and even compiles
- > >the program, but it is unpredictable what will occur)
- > >on it:
-
- Klaus Eichele wrote:
- > Hi,
- > I am not sure what this program is supposed to achieve, but compiling
- > it sure produces an error: illegal initialization.
- > > static int p=a;
- > The reason is that at initialization time a is (or would be)
- > undefined. If you want your function to work, you will need to change
- > it to:
-
-
- Thanks to all, who reply on my message. I think i need to
- explain some things in hronological sequence.
- Once my friend wrote a program. He need to store incoming value
- in some function at first call and do something else with
- this value at next calls. He wants initialize 'static' variable
- with incoming value:
-
- int func( int i )
- {
- static int sv = a;
- ...
- }
-
- But i thought there are two ways:
- 1. it is impossible (complier initialize variable at start of
- whole program [i believe memory is allocated at that stage] and
- it doesn't know with what value to do it) -> ERROR ON COMPLILE.
- 2. compiler spend some resources to store is that was first or
- sequenced call and initializing is going normally -> COMPILE OK.
-
- We decided to check out what does complier. So we wrote simple
- program:
-
- #include <stdio.h>
- int func( int a )
- {
- static int sv = a;
- return sv++;
- }
- void main()
- {
- printf("%i\n", func(1) );
- }
-
- That compiles perfectly so we decided that compiler goes 2 way :).
- But we were still not sure that complier does all fine and make
- assembler source (compile via asm, generate asm source).
- And what did we see??? It initialize static int sv with 1!!!!!
- We were doubted. Then we begin modify our source to beat the
- compiler. Next program was:
-
- #include <stdio.h>
- int func( int a )
- {
- static int sv = a;
- return sv++;
- }
- void main( int argc, char *argv[] )
- {
- printf("%i\n", func(argc) );
- }
-
- And what do you think? Compiler initialyze (from asm source)
- static int sv with value is laying in address of argc. BTW all
- these initializations was done before main starts :).
-
- Last program which was represented in my first message beat compiler.
- It begins to print numbers begining with strange 825!!!
- (that was really so on my computer)
- I test last variant of program with SunOs compiler,
- it say an ERROR (1 way): non-constant initializer. I'v got that here
- is all right.
-
- All what was written here and in my first message is not stupid yell.
- But I was wondered why did you get normal results and I didn't.
-
- While i wrote this answer I tried to test all fragments with
- SunOs compiler. And, it's tariffic, I'v got what was a problem:
- C and C++ sources are generated different ways (it's my opinion).
- C: 1 way;
- C++: 2 way.
- Just I renamed my test file I got what I wait.
- Unfortunately I forgot what C or C++ source I generated with BC
- but I am sure bug WAS!!!
-
- Thanks to all for attention, that was my fault.
-